package hcdh.java.dathuc;

import hcdh.java.donthuc.DonThuc;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


public class DaThuc
{
    private List<DonThuc> m_danhSachDonThuc = new LinkedList<DonThuc>();
    
    public static void main(String[] args)
    {
        DaThuc daThuc1 = new DaThuc();
//        daThuc.nhap();
//        daThuc.chuanHoa();        
//        System.out.println(daThuc);

        daThuc1.add(new DonThuc(1.0, 2));
        daThuc1.add(new DonThuc(2.0, 4));
        daThuc1.add(new DonThuc(8.0, 2));
        daThuc1.add(new DonThuc(-22.0, 3));
        daThuc1.add(new DonThuc(-22.0, 11));
        System.out.println("Da Thuc (0):" + daThuc1.chuanHoa());
        
        
        DaThuc daThuc2 = new DaThuc();
        daThuc2.add(new DonThuc(5.0, 2));
        daThuc2.add(new DonThuc(1.0, 6));
        daThuc2.add(new DonThuc(8.0, 3));
        daThuc2.add(new DonThuc(-12.0, 6));
        System.out.println("Da Thuc (1): " + daThuc2.chuanHoa());
        
        System.out.println("(1) + (2) = " + daThuc1.cong(daThuc2));
    }

    public void setDanhSachDonThuc(List<DonThuc> m_danhSachDonThuc) {
        this.m_danhSachDonThuc = m_danhSachDonThuc;
    }

    public List<DonThuc> getDanhSachDonThuc() {
        return m_danhSachDonThuc;
    }

    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        
        System.out.print("So don thuc : ");
        int n = scan.nextInt();
        
        for (int i = 0; i < n; i++) {
            System.out.println("Don thuc (" + i + ")");
            DonThuc dt = new DonThuc();
            dt.nhap();
            m_danhSachDonThuc.add(dt);
        }
    }
    
    
    public void xuat() {
        this.toString();
    }
    
    public static void xuat(DaThuc dt) {
        dt.toString();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(get(0));
        for (int i =1; i < size(); i++) {
            DonThuc dt = get(i);
            sb.append(" ");
            if (dt.getA() > 0) {
                sb.append("+");
            }
            sb.append(dt);
        }
        return sb.toString();
    }
    
    public double tinh(double x) {
        double s = 0;
        for (DonThuc dt : getDanhSachDonThuc()) {
            s += dt.tinh(x);
        }
        return s;
    }
    
    public DaThuc tru(DaThuc daThuc) {
        Collections.sort(chuanHoa().getDanhSachDonThuc());
        Collections.sort(daThuc.chuanHoa().getDanhSachDonThuc());
        
        DaThuc req = new DaThuc();
        int n = size() > daThuc.size() ? daThuc.size() : size();
        for (int i = 0; i < n; i++) {
            req.add(get(i).tru(daThuc.get(i)));
        }
        
        for (int j = n; j < size(); j++) {
            req.add(get(j));
        }
        
        for (int k = n; k < daThuc.size(); k++) {
            req.add(daThuc.get(k));
        }
        
        return req;
    }
    
    /**
     * @param daThuc
     * @return
     */
    public DaThuc cong(DaThuc daThuc) {
        Collections.sort(chuanHoa().getDanhSachDonThuc());
        Collections.sort(daThuc.chuanHoa().getDanhSachDonThuc());
        
        DaThuc req = new DaThuc();
        int n = size() > daThuc.size() ? daThuc.size() : size();
        for (int i = 0; i < n; i++) {
            DonThuc dt = get(i).cong(daThuc.get(i));
            if (dt != null) {
                req.add(dt);
            } else {
                req.add(new DonThuc(get(i).getA(), get(i).getN()));
                req.add(new DonThuc(daThuc.get(i).getA(), daThuc.get(i).getN()));
            }
        }
        
        for (int j = n; j < size(); j++) {
            req.add(get(j));
        }
        
        for (int k = n; k < daThuc.size(); k++) {
            req.add(daThuc.get(k));
        }
        
        return req;
    }
    
    //TODO
    public void parse() {
    }
    
    public int size() {
        return getDanhSachDonThuc().size();
    }
    
    public DonThuc get(int i ) {
        return getDanhSachDonThuc().get(i);
    }
    
    public DaThuc add(DonThuc dt) {
        getDanhSachDonThuc().add(dt);
        return this;
    }
    
    public DaThuc chuanHoa() {
        Iterator<DonThuc> it = getDanhSachDonThuc().iterator();
        int n = getDanhSachDonThuc().size();
        for (int i = 0; i < n - 1; i++) {
            DonThuc dt1 = it.next(); //i
            for (int j = i + 1; j < n; j++) {
                DonThuc dt2 = getDanhSachDonThuc().get(j); //i+1
                if (dt1.getN() == dt2.getN()) {
                    dt2.setA(dt2.getA() + dt1.getA());
                    it.remove();
                    n--;
                }
            }
        }
        Collections.sort(getDanhSachDonThuc());
        return this;
    }

}
